//
//  AccountCenterProdItemView.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/19.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "AccountCenterProdItemView.h"
#define margintop 10
#define marginleft 10
#define cellTextColor JXColor(77, 77, 77)
#define cellHeight [DimensUtils getScaleX:57]

#define ACCOUNT_CENTER_PRODITEM_VIEW_NUM [DimensUtils getScaleX:15]
#define ACCOUNT_CENTER_PRODITEM_VIEW_ICON_H [DimensUtils getScaleY:36]
#define ACCOUNT_CENTER_PRODITEM_VIEW_ICON_W [DimensUtils getScaleX:36]

@interface AccountCenterProdItemView()
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *prodNameLabel;
@property(nonatomic,strong) UILabel *prodSumLabel;
@property(nonatomic,strong) UILabel *prodPriceLabel;
@end

@implementation AccountCenterProdItemView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //1.图片
        UIImageView *imageView = [[UIImageView alloc]init];
        [imageView setBackgroundColor:[UIColor redColor]];
        imageView.frame = CGRectMake(marginleft,margintop,ACCOUNT_CENTER_PRODITEM_VIEW_ICON_W,ACCOUNT_CENTER_PRODITEM_VIEW_ICON_H);
        //设置layer
        CALayer *layer=[imageView layer];
        //设置边框线的宽
        [layer setBorderWidth:0.5];
        //设置边框线的颜色
        [layer setBorderColor:[JXColor(242, 242, 242) CGColor]];
        self.imageView = imageView;
        [self addSubview:imageView];
        
        //3.商品数量
        NSString *prodSumText = @"0";
        UILabel *prodSumLabel = [[UILabel alloc]init];
        [prodSumLabel setTextAlignment:NSTextAlignmentRight];
        [prodSumLabel setText:prodSumText];
        [prodSumLabel setTextColor:JXColor(0, 0, 0)];
        [prodSumLabel setFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_PRODITEM_VIEW_NUM]];
        CGSize prodSumTextSize = [prodSumText sizeWithFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_PRODITEM_VIEW_NUM]];
        [prodSumLabel setFrame:CGRectMake(JXScreenW-17.5-21.5-55.5-10, (cellHeight-prodSumTextSize.height)/2, 17.5, prodSumTextSize.height)];
        self.prodSumLabel = prodSumLabel;
        [self addSubview:prodSumLabel];
        
        //4.商品价格
        NSString *prodPriceText = @"¥0";
        UILabel *prodPriceLabel = [[UILabel alloc]init];
        [prodPriceLabel setTextAlignment:NSTextAlignmentRight];
        [prodPriceLabel setText:prodSumText];
        [prodPriceLabel setTextColor:JXColor(0, 0, 0)];
        [prodPriceLabel setFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_PRODITEM_VIEW_NUM]];
        CGSize prodPriceTextSize = [prodPriceText sizeWithFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_PRODITEM_VIEW_NUM]];
        //需要动态指定frame
        [prodPriceLabel setFrame:CGRectMake(JXScreenW-marginleft-55.5, (cellHeight-prodPriceTextSize.height)/2, 55.5, prodPriceTextSize.height)];
        self.prodPriceLabel = prodPriceLabel;
        [self addSubview:prodPriceLabel];
        
        //2.商品名称
        NSString *prodNameText = @"商品名称";
        UILabel *prodNameLabel = [[UILabel alloc]init];
        [prodNameLabel setText:prodNameText];
        [prodNameLabel setTextColor:cellTextColor];
        [prodNameLabel setNumberOfLines:2];
        [prodNameLabel setFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_PRODITEM_VIEW_NUM]];
        [prodNameLabel setFrame:CGRectMake(CGRectGetMaxX(self.imageView.frame)+marginleft, margintop, CGRectGetMinX(self.prodSumLabel.frame)-CGRectGetMaxX(self.imageView.frame)-31.5, self.imageView.height)];
        self.prodNameLabel = prodNameLabel;
        [self addSubview:prodNameLabel];
        
        [self setFrame:CGRectMake(0, 0, JXScreenW, cellHeight)];
    }
    return self;
}

-(void)setCartInfos:(CartProdSub *)cartInfos{
  [self.imageView sd_setImageWithURL:[NSURL URLWithString:cartInfos.prodImageUrl]];
  if([cartInfos isPresent].integerValue == 1){//判断是否是赠品
      NSString *prodName = [NSString stringWithFormat:@"(赠)%@",cartInfos.prodName];
      NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:prodName];
      [str addAttribute:NSForegroundColorAttributeName value:JXThemeColor range:NSMakeRange(0,3)];
      NSString *prodPriceText = [NSString stringWithFormat:@"¥%@",cartInfos.prodNewPrice];
      [self.prodPriceLabel setText:prodPriceText];
      [self.prodNameLabel setAttributedText:str];
  }else if(cartInfos.prodPromotion && cartInfos.prodPromotion.price){// 判断是否是抢购商品
       NSString *prodPriceText = [NSString stringWithFormat:@"¥%@",cartInfos.prodPromotion.price];
       NSString *prodName = [NSString stringWithFormat:@"(抢)%@",cartInfos.prodName];
       NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:prodName];
      [str addAttribute:NSForegroundColorAttributeName value:JXThemeColor range:NSMakeRange(0,3)];
      [self.prodPriceLabel setText:prodPriceText];
      [self.prodNameLabel setAttributedText:str];
  }else{
      NSString *prodPriceText = [NSString stringWithFormat:@"¥%@",cartInfos.prodNewPrice];
      [self.prodPriceLabel setText:prodPriceText];
      [self.prodNameLabel setText:cartInfos.prodName];
  }

  NSString *prodSumText = [NSString stringWithFormat:@"%@",cartInfos.prodCount];
  [self.prodSumLabel setText:prodSumText];
    
}

@end
